package com.nk.system.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.nk.system.beans.HistoryBean;
import com.nk.system.beans.UsersBean;
import com.nk.system.service.AuthenticationService;
import com.nk.system.service.HistoryService;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private HistoryService historyService;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean result = login(request, response);
		
		if(result){
			request.getRequestDispatcher("mainAssessment.jsp").forward(request, response);
		}
		else{			
			response.sendRedirect("login.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		boolean result = login(request, response);
		
		if(result){
			request.getRequestDispatcher("mainAssessment.jsp").forward(request, response);
		}
		else{			
			response.sendRedirect("login.jsp");
		}
	}
	
	private boolean login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		String user = request.getParameter("username");
		String pass = request.getParameter("password");

		WebApplicationContext con = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
		AuthenticationService authenticationService = (AuthenticationService)con.getBean("authenticationService");
		historyService = (HistoryService)con.getBean("historyService");
		
		HttpSession userSess = request.getSession();
		if(userSess.getAttribute("login") != null) {
			int id = (int)userSess.getAttribute("userID");
			
			UsersBean usersBean = authenticationService.authentication2(id);
			userSess.setAttribute("users", usersBean);
			
			List<HistoryBean> histry = historyService.getHistry(id);
			
			request.setAttribute("histryList", histry);
			return true;
		}
		
		if(user == null || user.equals("") || pass == null || pass.equals("")) {
			return false;
		}
		
		UsersBean usersBean = authenticationService.authentication(user, pass);
		
		if(usersBean != null) {
			userSess.setAttribute("login", request.getParameter("username"));
			userSess.setAttribute("userID", usersBean.getUsId());
			userSess.setAttribute("users", usersBean);
			
			int id = (int)userSess.getAttribute("userID");
			
			List<HistoryBean> histry = historyService.getHistry(id);
			
			request.setAttribute("histryList", histry);
			return true;
		}
		return false;
	}

}
